/**
 * 添加或者修改页面
 */
var TemplateInfoInfoDlg = {
    data: {
        id: "",
        templateName: "",
        templateCode: "",
        fileName: "",
        finalName: "",
        filePath: "",
        createTime: "",
        createUser: "",
        updateTime: "",
        updateUser: ""
    }
};
var files = [];
layui.use(['form', 'admin', 'ax', 'laydate', 'upload'], function () {
    var $ = layui.jquery;
    var $ax = layui.ax;
    var form = layui.form;
    var admin = layui.admin;
    var upload = layui.upload;


    //选完文件后不自动上传
    upload.render({
        elem: '#selectFileBtn'
        , url: '/file/templateUploadFile' //改成您自己的上传接口
        , auto: false
        , accept: 'file'
        , exts: 'xls|xlsx'
        , bindAction: '#uploadFile'
        , choose: function (obj) {
            files = [];
            files.push(obj.pushFile());
            obj.preview(function (index, file, result) {
                $("#fileName").val(file.name);
            });
        }
        , before: function (obj) {
            // var formData= $("#templateInfoForm").serialize();
            var formData = form.val('templateInfoForm');
            this.data = formData;
        }
        , done: function (res) {
            if (res.code == 200) {
                Changein.success("保存成功！");
                //传给上个页面，刷新table用
                admin.putTempData('formOk', true);
                //关掉对话框
                admin.closeThisDialog();
            } else {
                Changein.error("文件上传失败！" + res.message, 5000);
            }
        },
        error: function (data) {
            Changein.error("文件上传失败！" + data.responseJSON.message, 5000);
        }
    });


    // 保存点击事件
    $('#saveBtn').click(function () {
        TemplateInfoInfoDlg.validateTemplateAddPageInfo();
    });

    /**
     * 点击提交
     *
     * @param data 点击按钮时候的行数据
     */
    var saveTemplateLock = false;
    TemplateInfoInfoDlg.validateTemplateAddPageInfo = function () {
        var flag = validataForm();
        if (!flag) {
            $("#validateBtn").click();
            return false;
        }
        if (files.length == 0) {
            Changein.error("请选择合适的文件！");
            return false;
        }
        var formData = form.val('templateInfoForm');
        var operation = function () {
            if (!saveTemplateLock) {
                saveTemplateLock = true;
                var ajax = new $ax(Changein.ctxPath + "/templateInfo/validateTemplate", function (data) {
                    if (data.code==200){
                        $("#uploadFile").click();
                        saveTemplateLock = false;
                    }else {
                        Changein.error("数据校验失败！" + data.message);
                    }
                }, function (data) {
                    saveTemplateLock = false;
                    Changein.error("数据校验失败!" + data.responseJSON.message + "!");
                });
                ajax.set(formData);
                ajax.start();
            }
        };
        if (!saveTemplateLock) {
            Changein.confirm("是否提交?", operation);
        } else {
            Changein.error("正在提交中，请勿重复操作！");
        }
        return false;
    };

    //校验表单
    function validataForm() {
        var templateName = $("#templateName").val();
        if (Changein.isNull(templateName)) {
            return false;
        }
        var templateCode = $("#templateCode").val();
        if (Changein.isNull(templateCode)) {
            return false;
        }
        return true;
    }

    //表单提交事件,只是做校验
    form.on('submit(btnSubmit)', function (data) {
        return false;
    });


});